 using BitBenderGames;
using DG.Tweening;
using System;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Mars.Got.Timer;

namespace Yoozoo.Gameplay.City
{
    public class CityRootLoadComponent : MonoBehaviour
    {
        // Floor节点
        public Transform rootFloorNode;
        // Floor数据
        public CityFloorDataConfig cityFloorDataConfig;
        // Floor数据字典
        private Dictionary<int, CityFloorAreaData> _cityFloorAreaDataMap;
        // CityWallComponent组件
        private CityWallComponent _cityWallComponent;

        void Awake()
        {
            _cityWallComponent = gameObject.GetComponentInChildren<CityWallComponent>();

            cityFloorDataConfig.Deserialize();
            _cityFloorAreaDataMap = new Dictionary<int, CityFloorAreaData>();
            for (int i = 0; i < cityFloorDataConfig.areas.Count; ++i)
            {
                CityFloorAreaData cityFloorAreaData = cityFloorDataConfig.areas[i];
                _cityFloorAreaDataMap.Add(cityFloorAreaData.areaId, cityFloorAreaData);
            }
        }

        /// <summary>
        /// 加载完成回调
        /// </summary>
        public void LoadComplete()
        {
            _cityWallComponent.HideAllWalls();
        }

        /// <summary>
        /// 设置CityWallComponent组件数据，并初始化
        /// </summary>
        /// <param name="areaId"></param>
        /// <param name="lockFloorNode"></param>
        /// <param name="unlockFloorNode"></param>
        /// <param name="unlockEffectNode"></param>
        public void InitCityWallComponentData(int areaId, int maxUnlockedAreaCount, Transform lockFloorNode, Transform unlockFloorNode, 
            Transform unlockEffectNode, Transform containerEffectNode)
        {
            // 设置区域各种节点的数据
            SetCityWallComponentData(areaId, lockFloorNode, unlockFloorNode, unlockEffectNode, containerEffectNode);
            // 初始化
            _cityWallComponent.InitAreaWall(areaId, maxUnlockedAreaCount);
        }

        /// <summary>
        /// 设置CityWallComponent组件数据
        /// </summary>
        /// <param name="areaId"></param>
        /// <param name="lockFloorNode"></param>
        /// <param name="unlockFloorNode"></param>
        /// <param name="unlockEffectNode"></param>
        /// <param name="containerEffectNode"></param>
        public void SetCityWallComponentData(int areaId, Transform lockFloorNode, Transform unlockFloorNode, 
            Transform unlockEffectNode, Transform containerEffectNode)
        {
            CityFloorAreaData cityFloorAreaData;
            if (_cityFloorAreaDataMap.TryGetValue(areaId, out cityFloorAreaData))
            {
                List<GameObject> showWalls = new List<GameObject>();
                for (int i = 0; i < cityFloorAreaData.showWallHierarchyPaths.Count; ++i)
                {
                    Transform wallNode = rootFloorNode.Find(cityFloorAreaData.showWallHierarchyPaths[i]);
                    if (wallNode)
                    {
                        showWalls.Add(wallNode.gameObject);
                    }
                }

                _cityWallComponent.SetAreaWallData(areaId, lockFloorNode?.gameObject, unlockFloorNode?.gameObject
                    , unlockEffectNode?.gameObject, showWalls, containerEffectNode?.gameObject);
            }
        }
        
    }
}
